package logic

import (
	"context"
	"errors"
	"shop-switcher/models/mysql"

	"shop-switcher/shoprpc/internal/svc"
	"shop-switcher/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *shoprpc.ShopAddRequest) (*shoprpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	shop := mysql.Shop{
		Model:     gorm.Model{},
		ShopName:  in.ShopName,
		ShopPrice: float32(in.ShopPrice),
		ShopNum:   in.ShopNum,
		Supplier:  in.Supplier,
		UserId:    in.UserId,
	}

	if err := shop.AddShop(mysql.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	return &shoprpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
